import { RedisModuleOptions } from '@nestjs-modules/ioredis';

export const redisConfig: RedisModuleOptions = {
  type: 'single',
  options: {
    host: process.env.REDIS_HOST || 'localhost',
    port: Number(process.env.REDIS_PORT) || 6379,
    password: process.env.REDIS_PASSWORD || '123456',
    db: Number(process.env.REDIS_DB) || 0,
    
    // 连接重试策略
    retryStrategy: (times: number) => {
      if (times > 10) {
        return null;
      }
      return Math.min(times * 1000, 5000);
    },

    // 命令重试策略
    maxRetriesPerRequest: 3,
    enableOfflineQueue: true,
    
    // 连接设置
    connectTimeout: 10000,
    commandTimeout: 5000,
    keepAlive: 10000,
    noDelay: true,
    
    // 重连设置
    reconnectOnError: (err) => {
      const targetError = 'READONLY';
      if (err.message.includes(targetError) || err.message.includes('ECONNRESET')) {
        return true;
      }
      return false;
    },
    
    // 自动重新订阅
    autoResubscribe: true,
    autoResendUnfulfilledCommands: true,
    
    // 其他优化设置
    enableReadyCheck: true,
    maxLoadingRetryTime: 5000,
    disconnectTimeout: 2000,
    lazyConnect: false,
    
    // 监控设置
    showFriendlyErrorStack: process.env.NODE_ENV !== 'production'
  }
};





